import { usePermission } from '@/hooks/web/usePermission';
import { useRole } from '@/hooks/web/useRole';

export const permission = {
	mounted(el, binding) {
		if (binding.value == undefined) return;
		const { hasPermission } = usePermission();
		if (!hasPermission(binding.value)) {
			// el.parentNode && el.parentNode.removeChild(el); // 关键代码, 没有权限则删除元素
			const value = el.parentNode.getAttributeNode('role')?.value;
			if ('none' === value) {
				el.parentNode.remove();
			} else {
				el.remove();
			}
		}
	},
};

export const role = {
	mounted(el, binding) {
		if (binding.value == undefined) return;
		const { hasRole } = useRole();
		if (!hasRole(binding.value)) {
			// el.parentNode && el.parentNode.removeChild(el); // 关键代码, 没有权限则删除元素
			const value = el.parentNode.getAttributeNode('role')?.value;
			if ('none' === value) {
				el.parentNode.remove();
			} else {
				el.remove();
			}
		}
	},
};
